<?php
/**
 * @see Alinks_Menu_Keyphrases
 */
require_once('Alinks/Menu/Keyphrases.php');

/**
 * @see Alinks_Menu_Settings
 */
require_once('Alinks/Menu/Settings.php');

/**
 * @see Alinks_Menu_ImportExport
 */
require_once('Alinks/Menu/ImportExport.php');

/**
 * @see Alinks_Menu_Factories
 */
require_once('Alinks/Menu/Factories.php');

/**
 * @see Alinks_Menu_Help
 */
require_once('Alinks/Menu/Help.php');

/**
 * @see Alinks_Popup
 */
require_once('Alinks/Popup.php');

/**
 * Sets up the admin menus
 *
 * Copyright (C) 2008  Sean Hickey
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Sean Hickey <sean@headzoo.com>
 * @version $Id: Menu.php 432 2008-07-13 21:00:48Z sean $
 * @package Alinks_Menu
 */
class Alinks_Menu
{
	/**
	 * Access level required to view the menus
	 *
	 * @var int
	 */
	const ACCESS_LEVEL = 10;
	
	const MENU_SETTINGS			= 'alinks_settings';
	const MENU_IMPORT_EXPORT	= 'alinks_import_export';
	const MENU_FACTORIES		= 'alinks_factories';
	const MENU_HELP				= 'alinks_help';
	
	/**
	 * The factory forms for either settings or keyphrases
	 *
	 * @var Alinks_Form[]
	 */
	protected $_forms = array();
	
	/**
	 * Calls the selected menu's init() method
	 */
	public function init()
	{
		if (isset($_GET['page']) && stripos($_GET['page'], 'alinks') !== false) {
			switch($_GET['page']) {
				case self::MENU_SETTINGS:
					add_action('admin_head', array('Alinks_Menu_Settings', 'head'));
					Alinks_Menu_Settings::init();
					break;
				case self::MENU_IMPORT_EXPORT:
					add_action('admin_head', array('Alinks_Menu_ImportExport', 'head'));
					Alinks_Menu_ImportExport::init();
					break;
				case self::MENU_FACTORIES:
					if (Alinks_Registry::get('alinks_mu') && !is_site_admin()) {
						return;
					}
					add_action('admin_head', array('Alinks_Menu_Factories', 'head'));
					Alinks_Menu_Factories::init();
					break;
				case self::MENU_HELP:
					add_action('admin_head', array('Alinks_Menu_Help', 'head'));
					Alinks_Menu_Help::init();
					break;
				default:
					add_action('admin_head', array('Alinks_Menu_Keyphrases', 'head'));
					Alinks_Menu_Keyphrases::init();
					break;
			}
		}
	}
	
	/**
	 * Sets up the menu to be displayed in the WordPress main
	 * tab bar
	 *
	 * @see http://codex.wordpress.org/Adding_Administration_Menus
	 */
	public function add()
	{	
		add_menu_page('aLinks: Keyphrases', 'aLinks', self::ACCESS_LEVEL,  __FILE__, array($this, 'main'));
		add_submenu_page(__FILE__, 'aLinks: Keyphrases', 'Keyphrases', self::ACCESS_LEVEL, __FILE__, array($this, 'main'));
		add_submenu_page(__FILE__, 'aLinks: Settings', 'Settings', self::ACCESS_LEVEL, self::MENU_SETTINGS, array($this, 'settings'));
		add_submenu_page(__FILE__, 'aLinks: Import/Export', 'Import/Export', self::ACCESS_LEVEL, self::MENU_IMPORT_EXPORT, array($this, 'importExport'));
		if (Alinks_Registry::get('alinks_mu') && is_site_admin()) {
			add_submenu_page(__FILE__, 'aLinks: Factories', 'Factories', self::ACCESS_LEVEL, self::MENU_FACTORIES, array($this, 'factories'));
		} else if (!Alinks_Registry::get('alinks_mu')) {
			add_submenu_page(__FILE__, 'aLinks: Factories', 'Factories', self::ACCESS_LEVEL, self::MENU_FACTORIES, array($this, 'factories'));
		}
		add_submenu_page(__FILE__, 'aLinks: Help', 'Help', self::ACCESS_LEVEL, self::MENU_HELP, array($this, 'help'));
		add_action('admin_notices', array($this, 'adminNotices'));
	}
	
	/**
	 * Puts a div tag in the admin notices area for Ajax messages
	 *
	 * Called by WordPress's admin_notices action.
	 *
	 * @see self::add()
	 */
	public function adminNotices()
	{
		echo '<div id="alinks_ajax_notices"></div>';
	}
	
	/**
	 * Shows the main menu
	 *
	 * Called by WordPress after registering the menu with add_submenu_page.
	 *
	 * @see self::add()
	 */
	public function main()
	{
		Alinks_Menu_Keyphrases::show();
	}
	
	/**
	 * Shows the settings menu
	 *
	 * Called by WordPress after registering the menu with add_submenu_page.
	 *
	 * @see self::add()
	 */
	public function settings()
	{
		Alinks_Menu_Settings::show();
	}
	
	/**
	 * Shows the import/export menu
	 *
	 * Called by WordPress after registering the menu with add_submenu_page.
	 *
	 * @see self::add()
	 */
	public function importExport()
	{
		Alinks_Menu_ImportExport::show();
	}
	
	/**
	 * Shows the factories menu
	 *
	 * Called by WordPress after registering the menu with add_submenu_page.
	 *
	 * @see self::add()
	 */
	public function factories()
	{
		Alinks_Menu_Factories::show();
	}
	
	/**
	 * Shows the help menu
	 *
	 * Called by WordPress after registering the menu with add_submenu_page.
	 *
	 * @see self::add()
	 */
	public function help()
	{
		Alinks_Menu_Help::show();
	}
}